import os, yaml, argparse

import logging
import ntu

class Generator():
    def __init__(self, args):
        self.dataset = args.dataset
        self.generator = ntu.create(args)

    def start(self):
        logging.info('')
        logging.info('Starting generating ...')
        logging.info('Dataset: {}'.format(self.dataset))
        self.generator.start()
        logging.info('Finish generating!')

def main():
    # Loading Parameters
    parser = init_parameters()
    args, _ = parser.parse_known_args()
    args = update_parameters(parser, args)
    g = Generator(args)
    g.start()



def init_parameters():
    parser = argparse.ArgumentParser(description='Skeleton-based Action Recognition Dataset')
    # Data Generate Setting
    parser.add_argument('--config', '-c', type=str, default='configs/ntud_dataset.yaml', help='name of the using config')
    parser.add_argument('--dataset', '-d', type=str, default= None, help='Select dataset')
    parser.add_argument('--data_path', type= list,default= None, help='Args for dataset path')
    parser.add_argument('--pre_normalization', type=bool, default= False, help='Args for whether pre_normalization data')
    parser.add_argument('--ignored_path', type=str, default= None, help='Args for creating dataset')
    parser.add_argument('--out_path', type=str, default= None, help='Args for saving dataset')
    return parser

def update_parameters(parser, args):
    if os.path.exists(args.config):
        with open(args.config, 'r') as f:
            try:
                yaml_arg = yaml.load(f, Loader=yaml.FullLoader)
            except:
                yaml_arg = yaml.load(f)
            default_arg = vars(args)
            for k in yaml_arg.keys():
                if k not in default_arg.keys():
                    raise ValueError('Do NOT exist this parameter {}'.format(k))
            parser.set_defaults(**yaml_arg)
    else:
        raise ValueError('Do NOT exist this file in \'configs\' folder: {}.yaml!'.format(args.config))
    return parser.parse_args()

if __name__ == '__main__':
    os.chdir(os.getcwd())
    main()
